﻿#region 头注释
/******************************************************************************
文件名称    :   ChemistryUrine.cs
创建日期    :   2011/5/19 21:24:32
功能描述    :   化验尿常实体类
修改日志    :   
    2011-05-19 21:24  马利
*******************************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Newtonsoft.Json;
using Dare.Utilities.Data;

namespace Stone.Components.Entities
{
    /// <summary>
    /// 化验尿常实体类
    /// </summary>
    public class ChemistryUrine : EntityBase<ChemistryUrine, long>
    {
        #region 构造函数

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public ChemistryUrine()
            : base()
        {
            base.xmlSerializer = new DefaultXmlSerializer(this);
            base.jsonSerializer = new DefaultJsonSerializer(this);
        }

        public override void Reset()
        {
            weight = null;
            ph = null;
            wbc = null;
            nitrite = null;
            protein = null;
            rbcul = null;
            rbchp = null;
            wbcul = null;
            wbchp = null;
            tcul = null;
            tchp = null;
            tube = null;
            crystal = null;
            comment = null;
            timeStamp = DateTime.MinValue;
        }

        public override ChemistryUrine CreateNew()
        {
            return new ChemistryUrine();
        }

        #endregion

        #region 属性

        /// <summary>
        /// 获取实体类型
        /// </summary>
        public override EntityType EntityType
        {
            get { return EntityType.ChemistryUrine; }
        }

        /// <summary>
        /// 获取或设置比重
        /// </summary>
        public decimal? Weight
        {
            get { return weight; }
            set
            {
                if (entityState == EntityState.Saved && weight != value) entityState = EntityState.Changed;
                weight = value;
            }
        }
        protected decimal? weight;

        /// <summary>
        /// 获取或设置酸碱度
        /// </summary>
        public decimal? Ph
        {
            get { return ph; }
            set
            {
                if (entityState == EntityState.Saved && ph != value) entityState = EntityState.Changed;
                ph = value;
            }
        }
        protected decimal? ph;

        /// <summary>
        /// 获取或设置白细胞
        /// </summary>
        public decimal? Wbc
        {
            get { return wbc; }
            set
            {
                if (entityState == EntityState.Saved && wbc != value) entityState = EntityState.Changed;
                wbc = value;
            }
        }
        protected decimal? wbc;

        /// <summary>
        /// 获取或设置亚硝酸盐
        /// </summary>
        public decimal? Nitrite
        {
            get { return nitrite; }
            set
            {
                if (entityState == EntityState.Saved && nitrite != value) entityState = EntityState.Changed;
                nitrite = value;
            }
        }
        protected decimal? nitrite;

        /// <summary>
        /// 获取或设置蛋白质
        /// </summary>
        public decimal? Protein
        {
            get { return protein; }
            set
            {
                if (entityState == EntityState.Saved && protein != value) entityState = EntityState.Changed;
                protein = value;
            }
        }
        protected decimal? protein;

        /// <summary>
        /// 获取或设置红细胞计数ul(仪器)
        /// </summary>
        public decimal? Rbcul
        {
            get { return rbcul; }
            set
            {
                if (entityState == EntityState.Saved && rbcul != value) entityState = EntityState.Changed;
                rbcul = value;
            }
        }
        protected decimal? rbcul;

        /// <summary>
        /// 获取或设置红细胞计数HP(仪器)
        /// </summary>
        public decimal? Rbchp
        {
            get { return rbchp; }
            set
            {
                if (entityState == EntityState.Saved && rbchp != value) entityState = EntityState.Changed;
                rbchp = value;
            }
        }
        protected decimal? rbchp;

        /// <summary>
        /// 获取或设置白细胞计数ul(仪器)
        /// </summary>
        public decimal? Wbcul
        {
            get { return wbcul; }
            set
            {
                if (entityState == EntityState.Saved && wbcul != value) entityState = EntityState.Changed;
                wbcul = value;
            }
        }
        protected decimal? wbcul;

        /// <summary>
        /// 获取或设置白细胞计数HP(仪器)
        /// </summary>
        public decimal? Wbchp
        {
            get { return wbchp; }
            set
            {
                if (entityState == EntityState.Saved && wbchp != value) entityState = EntityState.Changed;
                wbchp = value;
            }
        }
        protected decimal? wbchp;

        /// <summary>
        /// 获取或设置管型计数ul(仪器)
        /// </summary>
        public decimal? Tcul
        {
            get { return tcul; }
            set
            {
                if (entityState == EntityState.Saved && tcul != value) entityState = EntityState.Changed;
                tcul = value;
            }
        }
        protected decimal? tcul;

        /// <summary>
        /// 获取或设置管型计数LP(仪器)
        /// </summary>
        public decimal? Tchp
        {
            get { return tchp; }
            set
            {
                if (entityState == EntityState.Saved && tchp != value) entityState = EntityState.Changed;
                tchp = value;
            }
        }
        protected decimal? tchp;

        /// <summary>
        /// 获取或设置病理管型
        /// </summary>
        public decimal? Tube
        {
            get { return tube; }
            set
            {
                if (entityState == EntityState.Saved && tube != value) entityState = EntityState.Changed;
                tube = value;
            }
        }
        protected decimal? tube;

        /// <summary>
        /// 获取或设置结晶
        /// </summary>
        public decimal? Crystal
        {
            get { return crystal; }
            set
            {
                if (entityState == EntityState.Saved && crystal != value) entityState = EntityState.Changed;
                crystal = value;
            }
        }
        protected decimal? crystal;

        /// <summary>
        /// 获取或设置备注
        /// </summary>
        public string Comment
        {
            get { return comment; }
            set
            {
                if (entityState == EntityState.Saved && comment != value) entityState = EntityState.Changed;
                comment = value;
            }
        }
        protected string comment;

        /// <summary>
        /// 获取或设置更新时间戳
        /// </summary>
        public DateTime TimeStamp
        {
            get { return timeStamp; }
            set
            {
                if (entityState == EntityState.Saved && timeStamp != value) entityState = EntityState.Changed;
                timeStamp = value;
            }
        }
        protected DateTime timeStamp;

        #endregion

        #region 方法

        #endregion

        #region 实体复制相关方法

        /// <summary>
        /// 复制当前实体属性到指定实体上
        /// </summary>
        /// <param name="entity">要把属性复制到的实体</param>
        /// <returns>返回传入的实体</returns>
        public override ChemistryUrine CopyTo(ChemistryUrine entity)
        {
            if (base.CopyTo(entity) == null) return null;

            entity.weight = this.weight;
            entity.ph = this.ph;
            entity.wbc = this.wbc;
            entity.nitrite = this.nitrite;
            entity.protein = this.protein;
            entity.rbcul = this.rbcul;
            entity.rbchp = this.rbchp;
            entity.wbcul = this.wbcul;
            entity.wbchp = this.wbchp;
            entity.tcul = this.tcul;
            entity.tchp = this.tchp;
            entity.tube = this.tube;
            entity.crystal = this.crystal;
            entity.comment = this.comment;
            entity.timeStamp = this.timeStamp;

            return entity;
        }

        #endregion

        #region 默认XML序列器类

        public class DefaultXmlSerializer : XmlSerializerBase, IEntityXmlSerializer<ChemistryUrine>
        {
            ChemistryUrine entity;
            public DefaultXmlSerializer(ChemistryUrine entity)
                : base(entity.GetType().Name)
            {
                this.entity = entity;
            }

            public ChemistryUrine Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            protected override void BeforeReadXml()
            {
                entity.Reset();
            }

            /// <summary>
            /// 读取XML属性
            /// </summary>
            /// <param name="name">属性名</param>
            /// <param name="value">属性值</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlAttribute(string name, string value, XmlReader reader)
            {
                switch (name)
                {
                    case "id":
                        entity.id = XmlConvert.ToInt64(value);
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(long.Parse(value));
                        break;
                    case "entityState":
                        entity.entityState = (EntityState)Enum.Parse(typeof(EntityState), value, true);
                        break;
                }
            }

            /// <summary>
            /// 读取XML子节点
            /// </summary>
            /// <param name="name">节点名</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlChildElement(string name, XmlReader reader)
            {
                switch (name)
                {
                    case "Weight":
                        entity.weight = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Ph":
                        entity.ph = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Wbc":
                        entity.wbc = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Nitrite":
                        entity.nitrite = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Protein":
                        entity.protein = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Rbcul":
                        entity.rbcul = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Rbchp":
                        entity.rbchp = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Wbcul":
                        entity.wbcul = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Wbchp":
                        entity.wbchp = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Tcul":
                        entity.tcul = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Tchp":
                        entity.tchp = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Tube":
                        entity.tube = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Crystal":
                        entity.crystal = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "Comment":
                        entity.comment = reader.ReadElementString();
                        break;
                }
            }

            /// <summary>
            /// 写入XML属性
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlAttributes(XmlWriter writer)
            {
                writer.WriteAttributeString("id", XmlConvert.ToString(entity.Id));
                writer.WriteAttributeString("timeStamp", entity.TimeStamp.Ticks.ToString());
                writer.WriteAttributeString("entityState", entity.EntityState.ToString());
            }

            /// <summary>
            /// 写入XML子节点
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlChildElements(XmlWriter writer)
            {
                if (entity.Weight != null) writer.WriteElementString("Weight", XmlConvert.ToString(entity.Weight.Value));
                if (entity.Ph != null) writer.WriteElementString("Ph", XmlConvert.ToString(entity.Ph.Value));
                if (entity.Wbc != null) writer.WriteElementString("Wbc", XmlConvert.ToString(entity.Wbc.Value));
                if (entity.Nitrite != null) writer.WriteElementString("Nitrite", XmlConvert.ToString(entity.Nitrite.Value));
                if (entity.Protein != null) writer.WriteElementString("Protein", XmlConvert.ToString(entity.Protein.Value));
                if (entity.Rbcul != null) writer.WriteElementString("Rbcul", XmlConvert.ToString(entity.Rbcul.Value));
                if (entity.Rbchp != null) writer.WriteElementString("Rbchp", XmlConvert.ToString(entity.Rbchp.Value));
                if (entity.Wbcul != null) writer.WriteElementString("Wbcul", XmlConvert.ToString(entity.Wbcul.Value));
                if (entity.Wbchp != null) writer.WriteElementString("Wbchp", XmlConvert.ToString(entity.Wbchp.Value));
                if (entity.Tcul != null) writer.WriteElementString("Tcul", XmlConvert.ToString(entity.Tcul.Value));
                if (entity.Tchp != null) writer.WriteElementString("Tchp", XmlConvert.ToString(entity.Tchp.Value));
                if (entity.Tube != null) writer.WriteElementString("Tube", XmlConvert.ToString(entity.Tube.Value));
                if (entity.Crystal != null) writer.WriteElementString("Crystal", XmlConvert.ToString(entity.Crystal.Value));
                if (entity.Comment != null) writer.WriteElementString("Comment", entity.Comment);
            }
        }

        #endregion

        #region 默认Json序列化器类

        public class DefaultJsonSerializer : JsonSerializerBase, IEntityJsonSerializer<ChemistryUrine>
        {
            ChemistryUrine entity;
            public DefaultJsonSerializer(ChemistryUrine entity)
                : base()
            {
                this.entity = entity;
            }

            public ChemistryUrine Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            /// <summary>
            /// 读取JSON属性
            /// </summary>
            /// <param name="propName">属性名称</param>
            /// <param name="propValue">属性值</param>
            /// <param name="reader">JSON读取器</param>
            protected override void ReadJsonProperty(string propName, object propValue, JsonReader reader)
            {
                switch (propName)
                {
                    case "id":
                        entity.id = DbConvert.ToNullableInt64(propValue) ?? entity.id;
                        break;
                    case "weight":
                        entity.weight = DbConvert.ToNullableDecimal(propValue) ?? entity.weight;
                        break;
                    case "ph":
                        entity.ph = DbConvert.ToNullableDecimal(propValue) ?? entity.ph;
                        break;
                    case "wbc":
                        entity.wbc = DbConvert.ToNullableDecimal(propValue) ?? entity.wbc;
                        break;
                    case "nitrite":
                        entity.nitrite = DbConvert.ToNullableDecimal(propValue) ?? entity.nitrite;
                        break;
                    case "protein":
                        entity.protein = DbConvert.ToNullableDecimal(propValue) ?? entity.protein;
                        break;
                    case "rbcul":
                        entity.rbcul = DbConvert.ToNullableDecimal(propValue) ?? entity.rbcul;
                        break;
                    case "rbchp":
                        entity.rbchp = DbConvert.ToNullableDecimal(propValue) ?? entity.rbchp;
                        break;
                    case "wbcul":
                        entity.wbcul = DbConvert.ToNullableDecimal(propValue) ?? entity.wbcul;
                        break;
                    case "wbchp":
                        entity.wbchp = DbConvert.ToNullableDecimal(propValue) ?? entity.wbchp;
                        break;
                    case "tcul":
                        entity.tcul = DbConvert.ToNullableDecimal(propValue) ?? entity.tcul;
                        break;
                    case "tchp":
                        entity.tchp = DbConvert.ToNullableDecimal(propValue) ?? entity.tchp;
                        break;
                    case "tube":
                        entity.tube = DbConvert.ToNullableDecimal(propValue) ?? entity.tube;
                        break;
                    case "crystal":
                        entity.crystal = DbConvert.ToNullableDecimal(propValue) ?? entity.crystal;
                        break;
                    case "comment":
                        entity.comment = Convert.ToString(propValue) ?? entity.comment;
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(DbConvert.ToNullableInt64(propValue) ?? 0);
                        break;
                }
            }

            /// <summary>
            /// 写入JSON属性
            /// </summary>
            /// <param name="writer">JSON编写器</param>
            protected override void WriteJsonProperties(JsonWriter writer)
            {

                //Id
                writer.WritePropertyName("id");
                writer.WriteValue(entity.Id);

                //比重
                writer.WritePropertyName("weight");
                if (entity.Weight == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Weight.Value);
                }

                //酸碱度
                writer.WritePropertyName("ph");
                if (entity.Ph == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Ph.Value);
                }

                //白细胞
                writer.WritePropertyName("wbc");
                if (entity.Wbc == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Wbc.Value);
                }

                //亚硝酸盐
                writer.WritePropertyName("nitrite");
                if (entity.Nitrite == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Nitrite.Value);
                }

                //蛋白质
                writer.WritePropertyName("protein");
                if (entity.Protein == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Protein.Value);
                }

                //红细胞计数ul(仪器)
                writer.WritePropertyName("rbcul");
                if (entity.Rbcul == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Rbcul.Value);
                }

                //红细胞计数HP(仪器)
                writer.WritePropertyName("rbchp");
                if (entity.Rbchp == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Rbchp.Value);
                }

                //白细胞计数ul(仪器)
                writer.WritePropertyName("wbcul");
                if (entity.Wbcul == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Wbcul.Value);
                }

                //白细胞计数HP(仪器)
                writer.WritePropertyName("wbchp");
                if (entity.Wbchp == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Wbchp.Value);
                }

                //管型计数ul(仪器)
                writer.WritePropertyName("tcul");
                if (entity.Tcul == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Tcul.Value);
                }

                //管型计数LP(仪器)
                writer.WritePropertyName("tchp");
                if (entity.Tchp == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Tchp.Value);
                }

                //病理管型
                writer.WritePropertyName("tube");
                if (entity.Tube == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Tube.Value);
                }

                //结晶
                writer.WritePropertyName("crystal");
                if (entity.Crystal == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Crystal.Value);
                }

                //备注
                writer.WritePropertyName("comment");
                if (entity.Comment == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Comment);
                }

                //更新时间戳
                writer.WritePropertyName("timeStamp");
                writer.WriteValue(entity.TimeStamp.Ticks);
            }
        }


        #endregion
    }
}